package com.github.ilioili.justdoit.components.activity;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.common.config.AnimationConfig;
import com.github.ilioili.justdoit.widget.CircleAnimationFrame;
import com.github.ilioili.justdoit.widget.WatchTimePicker;
import com.taihe.template.base.injection.Click;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import java.util.Calendar;

@Layout(R.layout.dialog_pick_time)
public class PickTimeActivity extends AppBaseActivity implements WatchTimePicker.OnTimeSelectListener {

    @Id(R.id.time_watch)
    private WatchTimePicker timePicker;
    @Id(R.id.tv_hour)
    private TextView tvHour;
    @Id(R.id.tv_minute)
    private TextView tvMinute;
    @Id(R.id.tv_am)
    private TextView tvAm;
    @Id(R.id.tv_pm)
    private TextView tvPm;
    @Id(R.id.circle_animation_frame)
    private CircleAnimationFrame circleAnimationFrame;

    private int hour;
    private int minute;
    private static WatchTimePicker.OnTimeSelectListener resultListener;


    @Override
    public void onTimeSelected(int hour, int minute) {
        this.minute = minute;
        if (tvPm.isSelected()) {
            this.hour = hour + 12;
        } else {
            this.hour = hour;
        }
        tvHour.setText(getFormatedStr(this.hour));
        tvMinute.setText(getFormatedStr(this.minute));
    }

    @Click(R.id.tv_am)
    private void onClickAm(View v) {
        if (hour > 11) {
            hour -= 12;
        }
        tvAm.setSelected(true);
        tvPm.setSelected(false);
        tvHour.setText(getFormatedStr(hour));
        tvMinute.setText(getFormatedStr(minute));
    }

    @Click(R.id.tv_pm)
    private void onClickPm(View v) {
        if (hour < 12) {
            hour += 12;
        }
        tvAm.setSelected(false);
        tvPm.setSelected(true);
        tvHour.setText(getFormatedStr(hour));
        tvMinute.setText(getFormatedStr(minute));
    }

    public void onClickCancel(View v) {
        circleAnimationFrame.collapse(CircleAnimationFrame.Gravity.Center, AnimationConfig.MEDIUM, true, new CircleAnimationFrame.CompleteListener() {
            @Override
            public void onComplete() {
                finish();
            }
        });
    }

    public void onClickConfirm(View v) {
        circleAnimationFrame.collapse(CircleAnimationFrame.Gravity.Center, AnimationConfig.MEDIUM, true, new CircleAnimationFrame.CompleteListener() {
            @Override
            public void onComplete() {
                if (null != resultListener) {
                    resultListener.onTimeSelected(hour, minute);
                }
                finish();
            }
        });
    }

    private String getFormatedStr(int num) {
        if (num < 10) {
            return "0" + num;
        } else {
            return "" + num;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timePicker.setOnTimeSelectListener(this);
        final Calendar calendar = Calendar.getInstance();
        final int curHour = calendar.get(Calendar.HOUR_OF_DAY);
        final int curMinute = calendar.get(Calendar.MINUTE);
        if (curHour > 11) {
            tvPm.setSelected(true);
            tvAm.setSelected(false);
        } else {
            tvPm.setSelected(false);
            tvAm.setSelected(true);
        }
        circleAnimationFrame.expand(CircleAnimationFrame.Gravity.Center, getResources().getInteger(android.R.integer.config_longAnimTime));
        timePicker.postDelayed(new Runnable() {
            @Override
            public void run() {
                final int hour = curHour > 11 ? curHour - 12 : curHour;
                timePicker.setTime(hour, curMinute, true);
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1).setDuration(getResources().getInteger(android.R.integer.config_longAnimTime));
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        onTimeSelected((int) (hour * value), (int) (curMinute * value));
                    }
                });
                valueAnimator.start();
            }
        }, 100);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
        resultListener = null;
    }

    @Override
    public void onBackPressed() {
        onClickCancel(null);
    }


    public static Intent newIntent(Context context, WatchTimePicker.OnTimeSelectListener onTimeSelectListener) {
        Intent intent = new Intent(context, PickTimeActivity.class);
        resultListener = onTimeSelectListener;
        return intent;
    }

}
